<?php
/**订单管理model*/
namespace App\Http\Models;

use App\Http\Controllers\CommonController;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Order extends Model
{
    protected $table='orders';
    /**
     * 用户订单管理列表
     * @userid 用户id
     */
    public static function lists($param)
    {
        $res = DB::table('orders as od')
            ->leftJoin('order_details as dt','od.id','=','dt.details_order_id')
            ->join('fitment_details as fit_dt','dt.details_fitment_id','=','fit_dt.details_id')
            ->leftJoin('fitments as ft','ft.fitments_id','=','fit_dt.details_pid')
            ->join('details_img as dt_img','fit_dt.details_id','=','dt_img.fitment_details_id')
            ->where('orders_user_id',$param['userid'])
            ->where('od.orders_isdel',1)
            ->where('dt.details_orders_isdel',1);
        if(isset($param['status']) && $param['status'] != ''){
            $res = $res->where('od.orders_status','=',$param['status']);
        }
        $res = $res->select(
            'od.id as order_id','od.orders_user_id','od.orders_orderNo','od.orders_status','od.orders_amount','od.created_at',
            'dt.id as dt_id','dt.details_fitment_id','dt.details_fitment_number','dt.details_rent_type',
            'ft.fitments_name', 'fit_dt.details_name','fit_dt.details_bond','fit_dt.details_fixprice','fit_dt.details_nimbleprice', 'fit_dt.details_id', 'fit_dt.details_length','fit_dt.details_width',
            'dt_img.fitment_details_imgOne'
        )
            ->orderBy('created_at','desc')
            ->paginate($param['pagesize']);
        return $res;
    }

    /**
     * 【购物车过来制单】
     * @param购物车数据
     * 额度扣除可扣的记录，混合需求的现金支付不考虑，这里没有真正完成订单，不需要考虑
     */
    public static function cartorder($param)
    {
        DB::beginTransaction();
            try{
            $res = DB::table('cart')->whereIn('cart_id',$param['cartid'])->get();
            if(count($res)>0){
                $param['orders_number'] = count($param['cartid']);
                $order = DB::table('orders')->insertGetId([
                        'orders_orderNo' => CommonController::orderNo(),
                        'orders_number' => $param['orders_number'],
                        'orders_user_id' => $param['userid'],
                        'orders_user_name' => $param['orders_user_name'],
                        'orders_status' => $param['orders_status'],
                        'orders_addr_id' => $param['orders_addr_id'],
                        'orders_count_bond' => $param['orders_count_bond'],
                        'orders_quota' => $param['orders_quota'],
                        'orders_deposite' => $param['orders_deposite'],
                        'orders_rentbill' => $param['orders_rentbill'],
                        'orders_discount' => $param['orders_discount'],
                        'orders_amount' => $param['orders_amount'],
                        'orders_user_require' => $param['orders_user_require'],
                        'orders_from'=>1,
                        'orders_isdel'=>1,
                        'created_at' => $param['created_at'],
                        'updated_at' => $param['updated_at']
                ]);
                if($order<1){
                    DB::rollback();
                    return '订单插入失败!';
                }
                //优惠券使用记录
                if(isset($param['coupon_dtid']) && $param['coupon_dtid']!=''){
                    $coupondt_insert = DB::table('coupon_detail')->insertGetId([
                        'user_coupon_id' => $param['user_coupon_id'],
                        'couponid'=>$param['coupon_dtid'],
                        'userid'=>$param['userid'],
                        'orderid'=>$order,
                        'number'=>1,
                        'status'=>1,
                        'isdel'=>1,
                        'created_at'=>date('Y-m-d H:i:s',time()),
                        'updated_at'=>date('Y-m-d H:i:s',time())
                    ]);
                    if($coupondt_insert<1){
                        DB::rollback();
                        return '使用优惠券记录失败!';
                    }
                    $coupondt_insert = DB::table('user_coupon')->where('id',$param['user_coupon_id'])->update(['status'=>0]);
                    if(!$coupondt_insert){
                        DB::rollback();
                        return '更新用户优惠券表数据失败!';
                    }
                }
                //信用额度计算
                if(isset($param['orders_quota']) && $param['orders_quota']>0){ //有使用额度积分消费
                    $quota_message = DB::table('quota')->where('quota_user_id',session('user')->id)->first();
                    $use_quota_value = $param['orders_quota'];
                    $back_quota_date = [
                        'quota_current'=>$quota_message->quota_current-$use_quota_value,
                        'quota_used'=>$quota_message->quota_used+$use_quota_value
                    ];
                    $back_quota_update = DB::table('quota')->where('quota_user_id',session('user')->id)->update($back_quota_date);
                    if(!$back_quota_update){
                        DB::rollback();return '抵扣信用额度扣除失败!';
                    }
                    $insert = [
                        'quota_detail_quota_id'=>$quota_message->quota_id,
                        'quota_detail_order_id'=>$order,
                        'quota_detail_amount'=>$param['orders_quota'],
                        'quota_detail_status'=>1,
                        'quota_detail_isdel'=>1,
                        'created_at'=>date('Y-m-d H:i:s',time()),
                        'updated_at'=>date('Y-m-d H:i:s',time())
                    ];
                    $quota_dt = DB::table('quota_detail')->insertGetId($insert);
                    if($quota_dt<1){
                        DB::rollback();return '订单结算信用额度主详情表记录扣除额度值失败!';
                    }
                }

               foreach ($res as $v){
                    $insert =  [
                        'details_orderNo' => CommonController::orderNo(),
                        'details_order_id' => $order,
                        'details_fitment_id' => $v->cart_fitment_id,
                        'details_fitment_number' => $v->cart_fitment_num,//下单数量
                        'details_rent_type' => $v->cart_rent_type,
                        'details_fitment_start' => date('Y-m-d',strtotime($v->cart_use_time)),
                        'rent_num'=>$v->cart_fitment_num,//在租数量
                        'sublet_num'=>null,//转租数量
                        'outrent_num'=>null,//退租数量
                        'repair_num'=>null,//售后数量
                        'details_orders_isdel' => 1,
                        'details_status' => 0,
                        'created_at' => date('Y-m-d H:i:s',time()),
                        'updated_at' => date('Y-m-d H:i:s',time())
                        ];
                   if($v->cart_rent_type==1){
                       $insert['details_fitment_end'] = date('Y-m-d',strtotime('+1 year',strtotime($v->cart_use_time)));
                   }else{
                       $insert['details_fitment_end'] = date('Y-m-d',strtotime('+18 months',strtotime($v->cart_use_time)));
                   }
                    $orderdt = DB::table('order_details')->insertGetId($insert);
                    $cart = DB::table('cart')->where('cart_id',$v->cart_id)->delete();
                    if(!$orderdt){
                        DB::rollback();return '订单详情插入失败!';
                    }
                }
            } else {
                DB::rollback();return '购物没有记录!';
            }
            DB::commit();
            $result = ['res'=>'ok','msg'=>$order];
            return $result;
        }catch(Exception $e){
            DB::rollback();return '下单失败!';
        }
    }

    /**
     * 【转租商品过来制单】
     * @param转租过来数据
     */
    public static function outrent_insertorder($param)
    {
        DB::beginTransaction();
        try{
            /**订单数据插入*/
            $order = DB::table('orders')->insertGetId(
                [
                    'orders_orderNo' => CommonController::orderNo(),
                    'orders_number' => $param['orders_number'],
                    'orders_user_id' => $param['userid'],
                    'orders_user_name' => session('user')->name,
                    'orders_status' => 1,
                    'orders_addr_id' => $param['orders_addr_id'],
                    'orders_count_bond' => $param['orders_count_bond'],
                    'orders_quota' => $param['orders_quota'],
                    'orders_deposite' => $param['orders_deposite'],
                    'orders_rentbill' => $param['orders_rentbill'],
                    'orders_discount' => $param['orders_discount'],
                    'orders_amount' => $param['orders_amount'],
                    'orders_user_require' => null,
                    'orders_from'=>2,
                    'orders_isdel'=>1,
                    'created_at' => $param['created_at'],
                    'updated_at' => $param['updated_at']
                ]
            );
            if(!$order){
                DB::rollback();
                return '订单插入失败';
            }
            /**订单详情数据插入*/
            $insert =  [
                'details_orderNo' => CommonController::orderNo(),
                'details_order_id' => $order,
                'details_fitment_id' => $param['fitment_details_id'],
                'details_fitment_number' => $param['orders_number'],//转租数量
                'details_rent_type' => 1,//1随租2租满即送
                'details_fitment_start' => date('Y-m-d',time()),
                'rent_num'=>$param['orders_number'],//在租商品数量
                'sublet_num'=>null,//转租数量
                'outrent_num'=>null,//退租
                'repair_num'=>null,//售后
                'details_orders_isdel' => 1,
                'details_status' => 1,//订单详情状态0未付款,1在租,2维修,3退租
                'created_at' => date('Y-m-d H:i:s',time()),
                'updated_at' => date('Y-m-d H:i:s',time())
            ];
            $insert['details_fitment_end'] = date('Y-m-d',strtotime('+12 months'));
            $orderdt = DB::table('order_details')->insertGetId($insert);
            if(!$orderdt){
                DB::rollback();
                return '订单详情插入失败';
            }
            DB::commit();
            $result = ['res'=>'ok','msg'=>$order];
            return $result;
        }catch(Exception $e){
            DB::rollback();
            return '下单失败';
        }
    }

    /**
     * 订单明细
     * @id 订单id
     */
    public static function orderdt($id)
    {
        $res =  DB::table('orders as od')
            ->leftJoin('order_details as od_dt','od_dt.details_order_id','=','od.id')
            ->join('fitment_details as fit_dt','fit_dt.details_id','=','od_dt.details_fitment_id')
            ->join('details_img as dt_img','dt_img.fitment_details_id','=','fit_dt.details_id')
            ->leftJoin('address as adr','adr.addr_id','=','od.orders_addr_id')
            ->where('od.id',$id)
            ->where('od.orders_isdel',1)
            ->select(
                'od.orders_orderNo','od.created_at','od.orders_status','od.orders_quota','od.orders_count_bond','od.orders_amount',
                'od.orders_discount','od.orders_paytype','od.orders_rentbill','od.orders_deposite','od.updated_at',
                'od_dt.details_fitment_number','od_dt.details_fitment_start','od_dt.details_fitment_end','od_dt.details_rent_type',
                'od_dt.details_orders_isdel','od_dt.details_orderNo',
                'fit_dt.details_name','fit_dt.details_length','fit_dt.details_width','fit_dt.details_height','fit_dt.details_describe',
                'fit_dt.details_bond','fit_dt.details_fixprice','fit_dt.details_nimbleprice',
                'dt_img.fitment_details_imgOne','dt_img.fitment_details_imgMaster',
                'adr.addr_rename','adr.addr_rephone','adr.addr_area','adr.addr_city','adr.addr_town','addr_detail'
            )
            ->get();
        return $res;
    }

    /**用户订单可删除判断*/
    public static function orderexist($userid,$orderdtid)
    {
        $res = DB::table('order_details as dt')
            ->join('orders as od','dt.details_order_id','=','od.id')
            ->where('dt.id',$orderdtid)
            ->where('od.orders_user_id',$userid)
            ->select('od.id')
            ->first();
        return $res;
    }

    /**
     * 【删除订单(只有未付款可删除)】
     * @orderid
     * 返回使用的优惠券和额度
     * 返回额度【优先删除钱抵扣的额度分，后退额度分】
     */
    public static function destroy($orderid,$orderdtid=null)
    {
        /**校验订单是否含有该详情单号外记录*/
        $exist = DB::table('orders as od')->leftJoin('order_details as dt','od.id','=','dt.details_order_id')->where('od.id',$orderid)->where('dt.id','<>',$orderdtid)
            ->get();
        /**订单详情关联订单信息*/
        $orders_message = DB::table('orders as od')
            ->leftJoin('order_details as od_dt','od_dt.details_order_id','=','od.id')
            ->leftJoin('coupon_detail as cp_dt','cp_dt.orderid','=','od.id')
            ->leftJoin('fitment_details as fit_dt','fit_dt.details_id','=','od_dt.details_fitment_id')
            ->leftJoin('coupon as cp','cp.coupon_id','=','cp_dt.couponid')
            ->where('od_dt.id',$orderdtid)
            ->select(
                'od.id as orderid','od_dt.id as orderdtid','od.orders_number','od.orders_rentbill','od.orders_deposite',
                'od.orders_count_bond','od.orders_quota','od.orders_rentbill','od.orders_discount','od.orders_rentbill','od.orders_amount',
                'od_dt.details_fitment_number','od_dt.rent_num','od_dt.details_rent_type',
                'fit_dt.details_bond', 'fit_dt.details_fixprice','fit_dt.details_nimbleprice',
                'cp_dt.couponid','cp_dt.user_coupon_id', 'cp.coupon_type','cp.coupon_cash',
                'cp.coupon_full','cp.coupon_cut'
            )
            ->first();
        /**还有其他订单未结算详情只删除这个,否则连同订单号删除*/
        if($exist){
            //订单表更新数据字段
            $order_update_data =[
                'orders_count_bond'=>$orders_message->orders_count_bond-($orders_message->details_bond*$orders_message->rent_num),
                'orders_number'=>$orders_message->orders_number-$orders_message->rent_num
            ];
            if($orders_message->details_rent_type==1){  //随租随还更新租金和押金数据
                $order_update_data['orders_rentbill']=$orders_message->orders_rentbill-($orders_message->rent_num*$orders_message->details_nimbleprice);
                $order_update_data['orders_deposite']=$orders_message->orders_deposite-($orders_message->rent_num*$orders_message->details_nimbleprice*2);
            }else{ //租满即送更新租金数据
                $order_update_data['orders_rentbill']=$orders_message->orders_rentbill-($orders_message->rent_num*$orders_message->details_fixprice);
                $order_update_data['orders_deposite']=$orders_message->orders_deposite;
            }
            DB::beginTransaction();
            try{
                if($orders_message->user_coupon_id>0){  //有使用优惠券情况
                    if($orders_message->coupon_type>1){ //使用现金抵用券【只抵用租金，大于租金时免租金】
                        if($orders_message->coupon_cash>$order_update_data['orders_rentbill']){  //抵用金额大于租金时，优惠价格数据更新
                            $order_update_data['orders_discount']=$order_update_data['orders_rentbill'];
                        }else{
                            $order_update_data['orders_discount']=$orders_message->coupon_cash;
                        }
                    }else{ //使用现金抵用券接受，使用满减优惠券开始
                        if($orders_message->coupon_full>$order_update_data['orders_rentbill']){ //可以参与满减
                            $order_update_data['orders_discount']=$orders_message->coupon_cut;
                        }else{ //优惠券使用状态变更
                            $user_coupon_update = DB::table('user_coupon')
                                ->where('id',function($query) use ($orders_message){
                                    $query->select('user_coupon_id')->from('coupon_detail')->where('orderid', $orders_message->orderid);
                                })->update(['status'=>1]);
                            if(!$user_coupon_update){
                                DB::rollback();return '优惠券使用状态变更失败!';
                            }
                            $coupon_dt_delete = DB::table('coupon_detail')->where('orderid',$orders_message->orderid)->delete();
                            if(!$coupon_dt_delete){
                                DB::rollback();return '优惠券使用详情删除失败!';
                            }
                        }
                    }
                }else{
                    $order_update_data['orders_discount']=0;
                }
                //额度积分处理【优先使用额度积分，减少其支付钱原则】
                if($order_update_data['orders_count_bond'] >= $orders_message->orders_quota){ //继续使用额度积分结算后面款项
                    $order_update_data['orders_quota'] = $orders_message->orders_quota;
                }else{ //需要返还信用度
                    $order_update_data['orders_quota'] = $order_update_data['orders_count_bond'];
                    $quota_message = DB::table('quota')->where('quota_user_id',session('user')->id)->select('quota_current','quota_used')->first();
                    //返还额度
                    $back_quota_value = $orders_message->orders_quota-$order_update_data['orders_count_bond'];
                    $back_quota_date = [
                        'quota_current'=>$quota_message->quota_current+$back_quota_value,
                        'quota_used'=>$quota_message->quota_used-$back_quota_value
                    ];
                    $back_quota_update = DB::table('quota')->where('quota_user_id',session('user')->id)->update($back_quota_date);
                    if(!$back_quota_update){
                        DB::rollback();return '额度值返回失败!';
                    }
                }
                //当前需支付金额数据更新
                $order_update_data['amount'] = $order_update_data['orders_rentbill']+$order_update_data['orders_deposite']-$order_update_data['orders_discount']+$order_update_data['orders_count_bond']-$order_update_data['orders_quota'];
                //订单表更新
                $orders_update = DB::table('orders')->where('id',$orders_message->orderid)->update($order_update_data);
                if(!$orders_update){
                    DB::rollback();return '主订单表更新失败!';
                }
                $dt = DB::table('order_details')->where('id',$orderdtid)->delete();
                if(!$dt){
                    DB::rollback();return '删除订单详情失败!';
                }
                DB::commit();return 'ok';
            }catch (Exception $e){
                DB::rollback();return '删除失败!';
            }
        }else{
            DB::beginTransaction();
            try{
                if($orders_message->orders_discount>0){ //有使用优惠券
                    $back_order_coupon = DB::table('user_coupon')->where('id',$orders_message->user_coupon_id)->update(['status'=>1]);
                    if(!$back_order_coupon){
                        DB::rollback();return '优惠券使用状态修改失败!';
                    }
                    //删除优惠券使用记录
                    $order_coupon_use_delete =  DB::table('coupon_detail')->where('orderid',$orders_message->orderid)->delete();
                    if(!$order_coupon_use_delete){
                        DB::rollback();return '优惠券使用记录删除失败!';
                    }
                }
                //校验是否有使用信用额度积分
                if($orders_message->orders_quota>0){
                    $quota_message = DB::table('quota')->where('quota_user_id',session('user')->id)->select('quota_current','quota_used')->first();
                    $back_quota_value = $orders_message->orders_quota;
                    $back_quota_date = [
                        'quota_current'=>$quota_message->quota_current+$back_quota_value,
                        'quota_used'=>$quota_message->quota_used-$back_quota_value
                    ];
                    $back_quota_update = DB::table('quota')->where('quota_user_id',session('user')->id)->update($back_quota_date);
                    if(!$back_quota_update){
                        DB::rollback();return '额度值返回失败!';
                    }
                }
                $dt = DB::table('order_details')->where('id',$orderdtid)->delete();
                if(!$dt){
                    DB::rollback();return '删除订单详情失败!';
                }else{
                    $od = DB::table('oders')->where('id',$orderid)->delete();
                    if(!$od){
                        DB::rollback();return '删除订单失败!';
                    }
                }
                DB::commit();return 'ok';
            }catch (Exception $e){
                DB::rollback();return '删除失败!';
            }
        }
    }

    /**
     * 【删除整体订单】
     * @orderid订单id
     * 优惠券返回
     * 信用额度返回【退额度分，删除需要现金的额度记录】
     */
    public static function destroys($orderid,$userid)
    {
        $re = DB::table('orders')->where('orders_status','0')->where('orders_user_id',$userid)->where('id',$orderid)
            ->get();
        if(count($re)>0){
            DB::beginTransaction();
            try{
                //删除订单前校验是否有使用优惠券
                if($re[0]->orders_discount>0){
                    $order_coupon_find = DB::table('coupon_detail')->where('orderid',$orderid)->first();
                    $back_order_coupon = DB::table('user_coupon')->where('id',$order_coupon_find->user_coupon_id)->update(['status'=>1]);
                    if(!$back_order_coupon){
                        DB::rollback();return '优惠券回退失败!';
                    }
                    //删除优惠券使用记录
                    $order_coupon_use_delete =  DB::table('coupon_detail')->where('orderid',$orderid)->delete();
                    if(!$order_coupon_use_delete){
                        DB::rollback();return '优惠券使用记录删除失败!';
                    }
                }
                //校验是否有使用信用额度积分
                if($re[0]->orders_quota>0){
                    $quota_message = DB::table('quota')->where('quota_user_id',session('user')->id)->select('quota_current','quota_used')->first();
                    $back_quota_value = $re[0]->orders_quota;
                    $back_quota_date = [
                        'quota_current'=>$quota_message->quota_current+$back_quota_value,
                        'quota_used'=>$quota_message->quota_used-$back_quota_value
                    ];
                    $back_quota_update = DB::table('quota')->where('quota_user_id',session('user')->id)->update($back_quota_date);
                    if(!$back_quota_update){
                        DB::rollback();return '额度值返回失败!';
                    }
                }
                $des = DB::table('orders')->where('id',$orderid)->delete();
                if(!$des) {
                    DB::rollback();return '订单表删除失败!';
                }
                $res = DB::table('order_details')->where('details_order_id',$orderid)->get();
                foreach ($res as $k=>$v) {
                    $de = DB::table('order_details')->where('id',$v->id)->delete();
                    if(!$de) {
                        DB::rollback();
                        return '订单详情表删除失败!';
                    }
                }
                DB::commit();return 'ok';
            }catch (Exception $e){
                DB::rollback();return '删除失败!';
            }
        }else{
           return '该订单不是未付款状态不能被删除!';
        }
    }

    /**
     * 【在租订单】
     * @param 查询条件
     */
    public static function rentorder($request)
    {
        $res = DB::table('orders as od')
            ->leftJoin('order_details as dt','od.id','=','dt.details_order_id')
            ->join('fitment_details as fit_dt','dt.details_fitment_id','=','fit_dt.details_id')
            ->join('details_img as img','dt.details_fitment_id','=','img.fitment_details_id')
            ->where('od.orders_isdel',1)
            ->where('od.orders_status','>',0)
            ->where('dt.details_orders_isdel',1);
        if(isset($request['userid']) && $request['userid'] != ''){
            $res = $res->where('od.orders_user_id',$request['userid']);
        }
        if(isset($request['status']) && $request['status'] != ''){
            $res = $res->where('dt.details_orders_isdel',$request['status']);
        }
        $res = $res->select(
            'fit_dt.details_name','fit_dt.details_fixprice','fit_dt.details_nimbleprice',
            'dt.details_rent_type','dt.details_fitment_number','dt.details_fitment_start',
            'dt.id', 'img.fitment_details_imgOne','fit_dt.details_id'
        );
        $res = $res->paginate($request['pagesize']);
        return $res;
    }

    /**订单详情明细*/
    public static function orderdetails($id,$param=null)
    {
        $res = DB::table('order_details as dt')
            ->leftJoin('orders as od','od.id','=','dt.details_order_id')
            ->join('fitment_details as fit_dt','fit_dt.details_id','=','dt.details_fitment_id')
            ->join('fitments as ft','ft.fitments_id','=','fit_dt.details_pid')
            ->join('details_img as img','img.fitment_details_id','=','dt.details_fitment_id')
            ->join('address as adr','adr.addr_id','=','od.orders_addr_id')
            ->where('dt.id',$id)
            ->where('dt.details_orders_isdel',1)
            ->select(
                'od.id','od.orders_orderNo',
                'dt.details_fitment_id','dt.created_at','dt.details_orderNo','dt.id as dtid',
                'dt.details_fitment_number', 'dt.details_rent_type','dt.details_fitment_start',
                'dt.details_fitment_end',
                'ft.fitments_name',
                'fit_dt.details_name','fit_dt.details_length','fit_dt.details_width','ft.fitments_name',
                'fit_dt.details_fixprice','fit_dt.details_nimbleprice','fit_dt.details_bond',
                'img.fitment_details_imgMaster','img.fitment_details_imgOne',
                'adr.addr_id as adr_id','adr.addr_rename','adr.addr_rephone', 'adr.addr_area',
                'adr.addr_city','adr.addr_town','adr.addr_detail'
            )
            ->first();
        return $res;
    }

    //用户消费订单
    public static function consume($param)
    {
        //查询字段名
        $sql_name = ' od.`orders_orderNo`,od.`orders_from`,od.`created_at`,od.`updated_at`,od_dt.`details_rent_type`,od_dt.`details_fitment_start`,od_dt.`details_fitment_id`,od_dt.`details_fitment_number`,ft.`fitments_name`,ft_dt.`details_name`,ft_dt.`details_fixprice`,ft_dt.`details_nimbleprice`,color.`color_type`,img.`fitment_details_imgMaster` ';
        //查询关联表
        $sql_tables = ' LEFT JOIN mzj_order_details AS od_dt ON od_dt.`details_order_id`=od.`id` LEFT JOIN mzj_details_img AS img ON img.`fitment_details_id`=od_dt.`details_fitment_id` LEFT JOIN mzj_fitment_details AS ft_dt ON ft_dt.`details_id`=od_dt.`details_fitment_id` LEFT JOIN mzj_fitments AS ft ON ft.`fitments_id`=ft_dt.`details_pid` LEFT JOIN mzj_fitment_color AS color ON color.`color_details_id`=ft_dt.`details_id` ';
        //查询条件
        $sql_where = ' WHERE od.`orders_user_id` = '.$param['userid'].' AND od.`orders_status` > 0 ';
        //排序
        $sql_orderby = ' ORDER BY od.`created_at` DESC ';
        //满足条件的数据
        $res_count = DB::select('SELECT count(od_dt.`id`) AS `num` FROM mzj_orders AS od '.$sql_tables.$sql_where.$sql_orderby);
        //满足条件分页数据
        $sql_limit = ' LIMIT '.$param['per_page'];
        if(isset($param['offset']) && $param['offset']!=''){
            $sql_offset = ' OFFSET '.$param['offset'];
        }else{
            $sql_offset = '';
        }
        $res_page = DB::select('SELECT '.$sql_name.' FROM mzj_orders AS od '.$sql_tables.$sql_where.$sql_orderby.$sql_limit.$sql_offset);
        $result = ['res_count'=>$res_count,'res_page'=>$res_page];
        return $result;
    }

}
